package com.kuang.web.admin;

import com.kuang.pojo.*;
import com.kuang.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class BlogController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;

    @Autowired
    private BlogTagService blogTagService;

    @Autowired
    private UserService userService;
    @RequestMapping("/toblog")
    public String toblog(Model model) {
        User user1 = userService.selectAdminUser(1);
        model.addAttribute("user1",user1);
        List<Type> typeList = typeService.selectAllType();
        List<Blog> blogList = blogService.selectAllBlog();
        int countBlog = blogService.countBlog();
//        int pageAll=
        model.addAttribute("countblog", countBlog);
        model.addAttribute("bloglist", blogList);
        model.addAttribute("typelist", typeList);
//        System.out.println(typeList);
        return "admin/blog-mgr.html";
    }

    @RequestMapping("/toaddblog")
    public String addblog(Model model) {
        User user1 = userService.selectAdminUser(1);
        model.addAttribute("user1",user1);
        List<Type> typeList = typeService.selectAllType();
        List<Tag> tagList = tagService.selectAllTag();
        model.addAttribute("taglist", tagList);
        model.addAttribute("typelist", typeList);
        return "admin/edit";
    }


    @RequestMapping("/addblog")
//    @ResponseBody
    public String addblog(Blog blog, String status, Integer[] tagId,Model model) {  // 从前端获取 标签的id 用来维护中间表 多对多
//        System.out.println(blog);
        User user1 = userService.selectAdminUser(1);
        model.addAttribute("user1",user1);
//        System.out.println(tagId);
        boolean published = status.equals("1") ? true : false;  //  是否发布
//        System.out.println(published);
        blog.setViews(0);
        blog.setUpdateTime(new Date());
        blog.setCreateTime(new Date());
        blog.setPublished(published);
        System.out.println("blog = " + blog);
        int num = blogService.insertBlog(blog);   // 先插入一篇博客，然后会自增一个id，然后再查询出这篇博客的id 再维护博客标签表
        Blog blog1 = blogService.selectBlogId();  // 查出最新的id
        for (int i = 0; i < tagId.length; i++) {   // 因为一篇博客对应多个标签，所以标签可能有1个或者说是多个,所以要用循环！
            blogTagService.insertBlogTag(blog1.getId(), tagId[i]);
        }
////        System.out.println(status);
        return "redirect:/admin/toblog";

    }

    @RequestMapping("/deleteblog")
    @ResponseBody
    public String deleteblog(String blogid,Model model) {
        User user1 = userService.selectAdminUser(1);
        model.addAttribute("user1",user1);
        int blog = blogService.deleteblog(Integer.parseInt(blogid));
        int blogtag = blogTagService.deleteByBlogId(Integer.parseInt(blogid));  //  删除博客表 同时删除中间表的数据
        if (blog > 0 && blogtag > 0) {
            return "success";
        } else {
            return "error";
        }
    }

    @RequestMapping("/tomodifyblog")
//    @ResponseBody
    public String tomodifyblog(Model model, HttpSession session, Integer blogid) {
        User user1 = userService.selectAdminUser(1);
//        model.addAttribute("user1",user1);
        session.setAttribute("user1",user1);
        Blog blog = blogService.selectBlogById(blogid);  // 修改页面上的数据回显
        List<Type> typeList = typeService.selectAllType();   ///  分类数据回显到下拉列表
        List<Tag> tagList = tagService.selectAllTag();///  标签数据回显到下拉列表
        Blog blog1 = blogService.selectBlogById(blogid);  // 通过博客id查出博客 查出该博客的分类
        Type type = new Type();
        type = typeService.selectTypeById(blog1.getTypeId());// 通过分类id查出分类 后面数据回显
        if (type == null) {  // 在删除分类表的记录，
            // 博客可能会有对应的记录 那么通过已经删除的分类id是不能查出来的，为了防止前台页面的空指针异常所以要重新创建对象
            type = new Type();
        }
        List<BlogTag> blogTagList = blogTagService.selectBlogTagByBlogId(blogid);// 通过博客id 查出中间表的记录 再通过tagid 查出对应的标签名
        Tag tag = new Tag();
        List<Tag> ataglist = new ArrayList<>(); // 为了存放通过博客标签中间表查出的标签id 然后查出标签表中的记录存放在这
        for (int i = 0; i < blogTagList.size(); i++) {
            BlogTag blogTag = blogTagList.get(i);    // 多条博客标签中间表的记录一个一个取出来,为了取出标签id
            if (blogTag.getTagsId() != 0) {
                tag = tagService.selectTagById(blogTag.getTagsId());// 通过标签id取出
            }
            ataglist.add(tag);   // 取出的对象存放在这
        }
        model.addAttribute("taglist", tagList);
        model.addAttribute("typelist", typeList);
        model.addAttribute("type1", type);
        model.addAttribute("blog", blog);
        model.addAttribute("ataglist", ataglist);
        System.out.println(blog);
        return "admin/tomodify";
    }

    //    @ResponseBody
    @RequestMapping("/modifyblog")
    public String modifyblog(Blog blog, String status, Integer[] tagId,Model model) {
//
        User user1 = userService.selectAdminUser(1);
        model.addAttribute("user1",user1);
        boolean published = status.equals("1") ? true : false;   // 发布还是不发布的状态
        blog.setPublished(published);
        blog.setUpdateTime(new Date());  // 修改时间
        System.out.println("blog = " + blog);
        for (int i = 0; i < tagId.length; i++) {
            System.out.println("----" + tagId[i]);
        }
        List<BlogTag> blogTags = blogTagService.selectIdByBlogId(blog); // 通过博客的id 查出中间表的所有博客id相同的记录，为了取出中间表的id
        System.out.println("11" + blogTags);
        blogTagService.deleteByBlogId(blog.getId());  // 先删除中间表的记录
        for (int i = 0; i < tagId.length; i++) {  // 再插入到中间表 大大简化开发
            if (tagId[i] != null) {  // 标签id不为空才插入博客标签中间表中
                int i1 = blogTagService.insertBlogTag(blog.getId(), tagId[i]);
            }
        }
        int i = blogService.updateBlogById(blog);
//        int num = blogService.updateBlogById(blog);  // 修改博客的信息  可能还会修改标签 标签和博客是多对多的关系，还要修改中间表
//        for (int i = 0; i < tagId.length; i++) {
//            int i1 = blogTagService.updateBlogTagById(blogTags.get(i).getId(), tagId[i]);  // 通过中间表的id修改相同博客对应的标签id
//        }
        return "redirect:/admin/toblog";

    }


}
